package com.example.myapplication1.ui.dashboard;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;
import androidx.annotation.Nullable;
import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProviders;

import com.example.myapplication1.Adapter.ContactAdapter;
import com.example.myapplication1.Bean.User;
import com.example.myapplication1.Main2Activity;
import com.example.myapplication1.R;

import org.litepal.LitePal;

import java.util.List;

public class DashboardFragment extends Fragment {
    private ListView  text_dashboard;
    List<User> userList;
    ContactAdapter adapter;

    public View onCreateView(@NonNull LayoutInflater inflater,
            ViewGroup container, Bundle savedInstanceState) {
      
        View view = inflater.inflate(R.layout.fragment_dashboard, container, false);
        initView(view);
        setListener();
        return view;
    }

    private void setListener() {
        text_dashboard.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                SharedPreferences sp=getActivity().getSharedPreferences("userinfo", Context.MODE_PRIVATE);
                SharedPreferences.Editor editor=sp.edit();
                User user=userList.get(position);
                editor.putInt("friendid",user.getId());
                editor.putString("friendname",user.getName());
                editor.apply();
                Intent it=new Intent(getContext(), Main2Activity.class);
                startActivity(it);
            }
        });

    }

    private void initView(View view) {
        userList= LitePal.findAll(User.class);
        text_dashboard=view.findViewById(R.id.text_dashboard);
        adapter=new ContactAdapter(getContext(),R.layout.contact_item,userList);
        text_dashboard.setAdapter(adapter);
    }
}